package com.ruoyi.web.controller.user;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUserUtils;
import com.ruoyi.user.domain.UserReceivingAli;
import com.ruoyi.user.service.UserReceivingAliService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收款方式-支付宝Controller
 *
 * @author ruoyi
 * @date 2024-01-10
 */
@Api(tags = "收款方式-支付宝")
@RestController
@RequestMapping("/user/receiving/ali")
public class UserReceivingAliController extends BaseController {
    @Autowired
    private UserReceivingAliService userReceivingAliService;

    /**
     * 查询收款方式-支付宝列表
     */
    @ApiOperation("查询收款方式-支付宝列表")
    @PostMapping("/list")
    public TableDataInfo list(UserReceivingAli userReceivingAli) {
        startPage();
        userReceivingAli.setUserId(SecurityUserUtils.getUserId());
        List<UserReceivingAli> list = userReceivingAliService.selectUserReceivingAliList(userReceivingAli);
        return getDataTable(list);
    }

    /**
     * 获取收款方式-支付宝详细信息
     */
    @ApiOperation("获取收款方式-支付宝详细信息")
    @PostMapping(value = "getInfo/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Long accountId) {
        UserReceivingAli ali = userReceivingAliService.selectUserReceivingAliByAccountId(accountId);
        if (!ali.getUserId().equals(SecurityUserUtils.getUserId())) {
            return success();
        }
        return success(ali);
    }

    /**
     * 新增收款方式-支付宝
     */
    @ApiOperation("新增收款方式-支付宝")
    @Log(title = "收款方式-支付宝", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody UserReceivingAli userReceivingAli) {
        userReceivingAli.setUserId(SecurityUserUtils.getUserId());
        return toAjax(userReceivingAliService.insertUserReceivingAli(userReceivingAli));
    }

    /**
     * 修改收款方式-支付宝
     */
    @ApiOperation("修改收款方式-支付宝")
    @Log(title = "收款方式-支付宝", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody UserReceivingAli userReceivingAli) {
        UserReceivingAli ali = userReceivingAliService.selectUserReceivingAliByAccountId(userReceivingAli.getAccountId());
        if (!ali.getUserId().equals(SecurityUserUtils.getUserId())) {
            return AjaxResult.error("无操作权限");
        }
        userReceivingAli.setUserId(SecurityUserUtils.getUserId());
        BeanUtils.copyProperties(userReceivingAli, ali, "accountId", "userId", "createTime");
        return toAjax(userReceivingAliService.updateUserReceivingAli(ali));
    }

    /**
     * 删除收款方式-支付宝
     */
    @ApiOperation("删除收款方式-支付宝")
    @Log(title = "收款方式-支付宝", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{accountIds}")
    public AjaxResult remove(@PathVariable Long[] accountIds) {
        return toAjax(userReceivingAliService.deleteUserReceivingAliByAccountIds(accountIds));
    }
}
